/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.auth', [
    "ngResource",
    "ngStorage"
])
    //认证服务
    .service('AuthService', ['$resource','$localStorage','$location',function($resource, $localStorage, $location){
        var $storage = $localStorage.$default({
            cmtuserId: '',
            cmtname: '',
            cmtwxpay_openid: '',
        });

        // 已登录则返回userid，否则跳转指定地址
        this.ensure_login = function() {
             if ($storage.cmtuserId) {
                 return $storage.cmtuserId;
             }
             alert("您还未登录,请先登录")
             window.location.href=projectBaseUrl+"login";
        }

        this.getUserid = function () {
            return $storage.cmtuserId;
        }
        this.getUsername = function () {
            return $storage.cmtname;
        }
        this.getWxopenid = function(){
            return $storage.cmtwxpay_openid;
        }

        this.setInfo = function(uid,uname,wxpay_openid){
            $storage.cmtname = uname;
            $storage.cmtuserId = uid;
            $storage.cmtwxpay_openid = wxpay_openid;

        }
    }])

    //用户地址服务
    .service('AddressService', ['$resource','AuthService',function($resource, AuthService){

        var address =  $resource(baseurl+'/usercontact/byuserid/:userid');
        //获取该用户的所有地址
        this.alladdress = function(){
            var userid = AuthService.getUserid();
            return address.query({userid:userid});
        }

    }])

    .service('FilterService', ['$resource',function($resource){

        this.transSex = function(input){
            var sex = null;
            if(input == '1'){
                sex = "先生";
            }else if(input == "2"){
                sex = "女士";
            }else{
                sex = input;
            }
            return sex;
        }
    }])
.filter('transsex', ['FilterService', function(FilterService) {
    return function(input) {
    	return FilterService.transSex(input);
    };
}])
;
